/**
 * @file test_relocate.c
 * @author liufeng(liufeng@injoinic.com)
 * @brief 测试简单的重定位功能
 * @version 0.1
 * @date 2024-11-23
 *
 * @copyright Copyright (c) 2024
 *
 */

#include "stdint.h"
#include "stdio.h"

extern int Load$$RW_IRAM1$$RW$$Base;
extern int Load$$RW_IRAM1$$RW$$Length;
extern int Load$$RW_IRAM1$$RW$$Limit;
extern int Load$$RW_IRAM1$$ZI$$Base;
extern int Load$$RW_IRAM1$$ZI$$Length;
extern int Load$$RW_IRAM1$$ZI$$Limit;

extern int Image$$RW_IRAM1$$RW$$Base;
extern int Image$$RW_IRAM1$$RW$$Length;
extern int Image$$RW_IRAM1$$RW$$Limit;
extern int Image$$RW_IRAM1$$ZI$$Base;
extern int Image$$RW_IRAM1$$ZI$$Length;
extern int Image$$RW_IRAM1$$ZI$$Limit;

void print_var_msg()
{
    printf("Load$$RW_IRAM1$$RW$$Base: [0x%x]\r\n", (uint32_t)&Load$$RW_IRAM1$$RW$$Base);
    printf("Load$$RW_IRAM1$$RW$$Length: %d\r\n", (uint32_t)&Load$$RW_IRAM1$$RW$$Length);
    printf("Load$$RW_IRAM1$$RW$$Limit: [0x%x]\r\n", (uint32_t)&Load$$RW_IRAM1$$RW$$Limit);
    printf("Load$$RW_IRAM1$$ZI$$Base: [0x%x]\r\n", (uint32_t)&Load$$RW_IRAM1$$ZI$$Base);
    printf("Load$$RW_IRAM1$$ZI$$Length: %d\r\n", (uint32_t)&Load$$RW_IRAM1$$ZI$$Length);
    printf("Load$$RW_IRAM1$$ZI$$Limit: [0x%x]\r\n\r\n", (uint32_t)&Load$$RW_IRAM1$$ZI$$Limit);

    printf("Image$$RW_IRAM1$$RW$$Base: [0x%x]\r\n", (uint32_t)&Image$$RW_IRAM1$$RW$$Base);
    printf("Image$$RW_IRAM1$$RW$$Length: %d\r\n", (uint32_t)&Image$$RW_IRAM1$$RW$$Length);
    printf("Image$$RW_IRAM1$$RW$$Limit: [0x%x]\r\n", (uint32_t)&Image$$RW_IRAM1$$RW$$Limit);
    printf("Image$$RW_IRAM1$$ZI$$Base: [0x%x]\r\n", (uint32_t)&Image$$RW_IRAM1$$ZI$$Base);
    printf("Image$$RW_IRAM1$$ZI$$Length: %d\r\n", (uint32_t)&Image$$RW_IRAM1$$ZI$$Length);
    printf("Image$$RW_IRAM1$$ZI$$Limit: [0x%x]\r\n", (uint32_t)&Image$$RW_IRAM1$$ZI$$Limit);
}

void rw_segment_copy()
{
    volatile uint8_t* srce = (uint8_t*)&Load$$RW_IRAM1$$RW$$Base;
    volatile uint8_t* dest = (uint8_t*)&Image$$RW_IRAM1$$RW$$Base;
    volatile int length = (int)&Load$$RW_IRAM1$$RW$$Length;

    for (volatile int i = 0; i < length; i++) {
        *dest = *srce;
        srce++;
        dest++;
    }
}

void zi_segment_zeroinit()
{
    volatile uint8_t* addr = (uint8_t*)&Image$$RW_IRAM1$$ZI$$Base;
    volatile int length = (int)&Image$$RW_IRAM1$$ZI$$Length;

    for (volatile int i = 0; i < length; i++) {
        *addr = 0;
        addr++;
    }
}
